package com.mindai.esper.monitor.biz.domain.assembler.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.mindai.esper.monitor.biz.domain.assembler.IAssembler;
import com.mindai.esper.monitor.inputadapters.model.EsperRequestAdapter;
import com.mindai.esper.monitor.inputadapters.model.SourceData;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/8/15.
 */
@Component
public class EsperRequestAdapterAssembler implements IAssembler<Message, EsperRequestAdapter> {
    @Override
    public EsperRequestAdapter assemble(Message message, EsperRequestAdapter esperRequestAdapter) {

        if (esperRequestAdapter == null) esperRequestAdapter = new EsperRequestAdapter();
        String jsonMessage = MsgConvertUtil.bytes2String(message.getBody(), "UTF-8");
        esperRequestAdapter.setJsonMessage(jsonMessage);

        SourceData sourceData = new SourceData();
        sourceData.setTopic(message.getTopic());
        sourceData.setTag(message.getTag());

        esperRequestAdapter.setSource(JSON.toJSONString(sourceData));
        return esperRequestAdapter;
    }
}
